package com.bgs.eat.commodity.service.impl;

import com.bgs.eat.commodity.entity.CommodityEntity;
import com.bgs.eat.commodity.mapper.CommodityMapper;
import com.bgs.eat.commodity.service.CommodityService;
import com.bgs.eat.config.CommonResult;
import com.bgs.eat.fileManager.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;


/**
 * @ClassName CommodityServiceImpl
 * @Description TODO
 * @Author lzy
 * @Date 2022/10/19 14:11
 **/
@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    CommodityMapper commodityMapper;

    @Autowired
    FileService fileService;

    /**
    * @Description 新增 商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/21 8:55
    * @Return com.bgs.eat.config.CommonResult
    * @param file
    * @param commodityEntity
    */
    @Override
    public CommonResult saveCommodity(MultipartFile file, CommodityEntity commodityEntity) {
        Integer imageId = fileService.uniUploadFile(file);
        commodityEntity.setImageId(imageId);
        commodityEntity.setCreateTime(new Date());
        // 默认 总销售量
        commodityEntity.setAmount(0);
        commodityEntity.setGoodCommentNum(0);
        commodityEntity.setBadCommentNum(0);
        commodityEntity.setMediumCommentNum(0);
        int insert = commodityMapper.insert(commodityEntity);
        if(insert > 0) {
            return CommonResult.ok(insert);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据 店铺id获取 商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/21 14:02
    * @Return com.bgs.eat.config.CommonResult
    * @param shopId
     * @param typeId
    */
    @Override
    public CommonResult getCommodityListByShopIdAndTypeId(Integer shopId, Integer typeId) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setShopId(shopId);
        commodityEntity.setTypeId(typeId);
        commodityEntity.setState(0);
        List<CommodityEntity> commodityEntities = commodityMapper.select(commodityEntity);
        return CommonResult.ok(commodityEntities);
    }

    /**
    * @Description 根据id获取商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/24 15:00
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult getCommodityListById(Integer id) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setId(id);
        CommodityEntity commodityEntity1 = commodityMapper.selectOne(commodityEntity);
        return CommonResult.ok(commodityEntity1);
    }

    /**
    * @Description 修改商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/24 19:43
    * @Return com.bgs.eat.config.CommonResult
    * @param file
    * @param commodityEntity
    */
    @Override
    public CommonResult editCommodity(MultipartFile file, CommodityEntity commodityEntity) {
        commodityEntity.setImageId(fileService.uniUploadFile(file));
        int i = commodityMapper.updateByPrimaryKeySelective(commodityEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 下架商品（门户商家版）
    * @Author lzy
    * @Date 2022/10/25 8:42
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult soldOutCommodity(Integer id) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setId(id);
        commodityEntity.setState(1);
        int i = commodityMapper.updateByPrimaryKeySelective(commodityEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据店铺id获取下架商品
    * @Author lzy
    * @Date 2022/10/25 8:56
    * @Return com.bgs.eat.config.CommonResult
    * @param shopId
    */
    @Override
    public CommonResult getSoldOutCommodityByShopId(Integer shopId) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setShopId(shopId);
        commodityEntity.setState(1);
        List<CommodityEntity> commodityEntities = commodityMapper.select(commodityEntity);
        return CommonResult.ok(commodityEntities);
    }

    /**
    * @Description 根据id商家商品（门户商家版）
    * @Author lzy
    * @Date 2022/10/25 9:02
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult putawayCommodity(Integer id) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setId(id);
        commodityEntity.setState(0);
        int i = commodityMapper.updateByPrimaryKeySelective(commodityEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 修改商品信息 无图片（门户商家版）
    * @Author lzy
    * @Date 2022/10/25 10:16
    * @Return com.bgs.eat.config.CommonResult
    * @param commodityEntity
    */
    @Override
    public CommonResult editCommodityNoImage(CommodityEntity commodityEntity) {
        int i = commodityMapper.updateByPrimaryKeySelective(commodityEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
    * @Description 根据id删除商品信息（门户商家版）
    * @Author lzy
    * @Date 2022/10/25 13:50
    * @Return com.bgs.eat.config.CommonResult
    * @param id
    */
    @Override
    public CommonResult deleteCommodityById(Integer id) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setId(id);
        int delete = commodityMapper.delete(commodityEntity);
        if (delete > 0) {
            return CommonResult.ok(delete);
        } else {
            return CommonResult.error("500");
        }
    }

    /**
     * @Description 修改 商品总数（门户商家版）
     * @Author lzy
     * @Date 2022/10/27 8:53
     * @Return com.bgs.eat.config.CommonResult
     * @param id
     * @param mode 0减商品总数(sum) 1加商品总数(sum)
     */
    @Override
    public CommonResult editCommoditySumById(Integer id, Integer mode) {
        Integer commoditySum = commodityMapper.selectCommoditySumById(id);
        if (commoditySum > 0){
            if (mode == 0) {
                Integer integer = commodityMapper.commoditySubtractSum(id);
                if (integer > 0) {
                    return CommonResult.ok(integer);
                } else {
                    return CommonResult.error("401");
                }
            } else if(mode == 1) {
                Integer integer = commodityMapper.commodityAddSum(id);
                if (integer > 0) {
                    return CommonResult.ok(integer);
                } else {
                    return CommonResult.error("401");
                }
            } else {
                return CommonResult.error("500");
            }
        } else {
            return CommonResult.error("401");
        }
    }

    /**
    * @Description 根据店铺id获取商品信息列表（门户用户版）
    * @Author lzy
    * @Date 2022/10/27 11:19
    * @Return com.bgs.eat.config.CommonResult
    * @param shopId
    */
    @Override
    public CommonResult getCommodityListAllByShopId(Integer shopId) {
        CommodityEntity commodityEntity = new CommodityEntity();
        commodityEntity.setShopId(shopId);
        List<CommodityEntity> commodityEntities = commodityMapper.select(commodityEntity);
        return CommonResult.ok(commodityEntities);
    }
}
